package BlackJack.Model.VO
{
	import BlackJack.View.Table;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import mx.events.MoveEvent;

	public class RoleClass
	{
		private var _name:String;
		/**	莊家只有point0...玩家有point0~3 */
		private var point:Vector.<MovieClip> = Vector.<MovieClip>([]);
		public var deal:MovieClip;
		private var dealend:MovieClip;
		/**	莊家只有deck0...玩家有deck0~3 */
		private var deck:Vector.<MovieClip>  = Vector.<MovieClip>([]);
		public var msg:MovieClip;
		private var count:int                = -1;
		public var poker:Vector.<Vector.<uint>>;
		private var _isSplit:Boolean;
		private var nPoint:MovieClip;
		private var pPoint:MovieClip;
		private var arrow:Vector.<MovieClip> = Vector.<MovieClip>([]);
		public var currentPoint:uint;
		public var index:uint;
		private var tid:uint;
		private var _data:Player;
		private var _model:MovieClip;
		private var _tip:SimpleButton;
		private var _betNumber:int;
		private var clipDict:Dictionary      = new Dictionary();
		public static const Insurance:String = 'Insurance';
		public static const Split:String     = 'Split';
		public var form:Dictionary;

		public function RoleClass(value:String)
		{
			_name = value;
			if (value == Table.Banker)
			{
				count = 1;
			}
			else
			{
				count = 3;
			}
			poker = new Vector.<Vector.<uint>>(count);
			for (var i:int = 0; i < count; i++)
			{
				poker[i] = Vector.<uint>([]);
			}
		}

		public function setTip(value:SimpleButton):void
		{
			if (_name == Table.Banker)
			{
				return;
			}
			value.focusRect = false;
			_tip = value;
			txtAccount = MovieClip(value.overState).Account;
			txtAccount.defaultTextFormat = txtAccount.getTextFormat();
			txtAccount.text = '';
		}
		public var txtAccount:TextField;

		public function setModel(value:MovieClip):void
		{
			if (_name == Table.Banker)
			{
				return;
			}
			value.focusRect = false;
			_model = value;
			txtName = value.Name;
			txtName.defaultTextFormat = txtName.getTextFormat();
			txtName.text = '';
			disable();
		}
		public var txtName:TextField;

		public function setModelNo(value:uint):void
		{
			_model.gotoAndStop('avatar' + value);
		}

		public function enable():void
		{
			_model.visible = true;
			_tip.visible = true;
		}

		public function disable():void
		{
			_model.visible = false;
			_tip.visible = false;
		}

		public function set data(value:Player):void
		{
			_data = value;
		}

		public function get data():Player
		{
			if (_data == null)
			{
				_data = new Player();
			}
			return _data;
		}

		public function get id():String
		{
			return data.ID;
		}

		public function clear():void
		{
			for (var i:int = 0; i < count; i++)
			{
				poker[i] = Vector.<uint>([]);
				point[i].gotoAndStop(1);
				Point(i).text = '';
				for (var j:int = 0; j < 20; j++)
				{
					var mc:MovieClip = Poker(i, j);
					if (mc)
					{
						mc.gotoAndStop(1);
					}
				}
			}
			if (msg)
			{
				msg.gotoAndStop(1);
			}
			if (nPoint)
			{
				nPoint.visible = false;
			}
			if (pPoint)
			{
				pPoint.visible = false;
			}
			isSplit = false;
		}

		public function dealPoker(value:uint, split:uint = 0):void
		{
			if (isSplit)
			{
				split += 1;
			}
			index = split;
			poker[split].push(value);
			deal.addEventListener('DealOK', dealOK);
			deal.gotoAndPlay('Start');
		}

		private function dealOK(event:Event):void
		{
			deal.removeEventListener('DealOK', dealOK);
			deal.gotoAndStop(1);
			var len:uint     = poker[index].length;
			var frame:String = poker[index][len - 1].toString(16).toUpperCase();
			dealend.gotoAndStop('Poker' + frame);
			clearTimeout(tid);
			tid = setTimeout(dealEnd, 150);
		}

		private function dealEnd():void
		{
			dealend.gotoAndStop(1);
			var len:uint = poker[index].length;
			setPoker(index, len - 1, poker[index][len - 1]);
			Point(index).text = countPoint(poker[index], true);
			deal.dispatchEvent(new Event('DealRoundOK'));
		}

		public function addPoker(value:uint, split:uint = 0):void
		{
			if (isSplit)
			{
				split += 1;
			}
			index = split;
			poker[split].push(value);
			var len:uint = poker[split].length;
			setPoker(split, len - 1, value);
			Point(split).text = countPoint(poker[split]);
		}

		public function getPoint(split:uint = 0):uint
		{
			if (isSplit)
			{
				split += 1;
			}
			var count:uint;
			var ace:Boolean;
			var len:uint = poker[split].length;
			var i:int;
			var u:uint;
			for (i = 0; i < len; i++)
			{
				u = poker[split][i];
				u = u % 16;
				u = u + 1;
				if (u > 10 && u < 14)
				{
					u = 10;
				}
				if (u == 14)
				{
					u = 1;
					ace = true;
				}
				count = count + u;
			}
			if (ace && count < 12)
			{
				count += 10;
			}
			return count;
		}

		public function countPoint(src:Vector.<uint>, effect:Boolean = false):String
		{
			var count:int;
			var len:uint        = src.length;
			var i:int;
			var temp:uint;
			var v:Vector.<uint> = Vector.<uint>([]);
			var ace:Boolean;
			var result:String;
			for (i = 0; i < len; i++)
			{
				temp = src[i] % 16;
				temp = temp + 1;
				if (temp < 14 && temp > 10)
				{
					temp = 10;
				}
				if (temp == 14)
				{
					temp = 1;
					ace = true;
				}
				count += temp;
				v.push(temp);
			}
			point[index].gotoAndStop('Single');
			result = count.toString();
			currentPoint = count;
			if (count > 21 && msg && effect)
			{
				msg.gotoAndStop('Bomb');
			}
			else if (count == 11 && ace)
			{
				currentPoint = count + 10;
				result = currentPoint.toString();
				if (msg && effect && len == 2)
				{
					msg.gotoAndStop('BlackJack');
				}
			}
			else if (count < 11 && ace)
			{
				point[index].gotoAndStop('Pair');
				result = count.toString() + '/' + (count + 10).toString();
				currentPoint = count + 10;
				if (_name == Table.Banker)
				{
					point[index].gotoAndStop('Single');
					result = currentPoint.toString();
				}
			}
			return result;
		}

		private function Poker(Index:uint, Pos:uint):MovieClip
		{
			return deck[Index]['Poker' + Pos];
		}

		private function Point(index:uint):TextField
		{
			return point[index].Label;
		}

		private function setPoker(Index:uint, Pos:uint, Point:int):void
		{
			if (Point == -1)
			{
				Poker(Index, Pos).gotoAndStop(1);
			}
			else
			{
				Poker(Index, Pos).gotoAndStop('Poker' + Point.toString(16).toUpperCase());
			}
		}

		public function setSubtotal(mc:MovieClip):void
		{
			if (mc == null)
			{
				return;
			}
			pPoint = mc.Positive;
			nPoint = mc.Negative;
			nPoint.visible = false;
			pPoint.visible = false;
		}

		public function setCalculation(point1:int, point2:int):void
		{
			if (isSplit)
			{
				setTargetClip(Split + 1, betNumber + point1);
				setTargetClip(Split + 2, betNumber + point2);
			}
			else
			{
				setTargetClip(Split + 0, betNumber + point1);
			}
		}

		public function setClip(mc:MovieClip):void
		{
			if (_name == Table.Banker)
			{
				return;
			}
			clipDict[Insurance] = mc.Insurance;
			var i:int;
			for (i = 0; i < 3; i++)
			{
				clipDict[Split + i] = mc[Split + i];
			}
		}

		private function setTargetClip(target:String, value:int):void
		{
			clearTargetClip(target);
			if (value <= 0)
			{
				return;
			}
			var mc:MultiClass = form[value];
			var len:uint      = mc.num.length;
			var num:uint;
			var count:uint;
			var i:int;
			var j:int;
			for (i = 0; i < len; i++)
			{
				num = mc.num[i];
				count = mc.count[i];
				for (j = 0; j < count; j++)
				{
					Clip(target, i, j).gotoAndStop('Clip' + num);
				}
			}
		}

		public function clearClip():void
		{
			if (_name == Table.Banker)
			{
				return;
			}
			clearTargetClip(Insurance);
			var i:int;
			for (i = 0; i < 3; i++)
			{
				clearTargetClip(Split + i);
			}
		}

		public function clearTargetClip(target:String):void
		{
			var i:int;
			var j:int;
			for (i = 0; i < 3; i++)
			{
				for (j = 0; j < 5; j++)
				{
					Clip(target, i, j).gotoAndStop(1);
				}
			}
		}

		public function Clip(target:String, clipNo:int, No:int):MovieClip
		{
			return clipDict[target]['Clip' + clipNo]['No' + No];
		}

		public function setArrow(mc:MovieClip):void
		{
			if (_name == Table.Banker)
			{
				return;
			}
			for (var i:int = 0; i < count; i++)
			{
				arrow[i] = mc['Arrow' + i];
				arrow[i].visible = false;
			}
		}

		public function arrowTo(value:int):void
		{
			if (_name == Table.Banker)
			{
				return;
			}
			arrow[0].visible = false;
			arrow[1].visible = false;
			arrow[2].visible = false;
			if (value == -1)
			{
				return;
			}
			if (isSplit)
			{
				value += 1;
			}
			arrow[value].visible = true;
		}

		public function set subtotal(value:int):void
		{
			var mc:MovieClip;
			if (value < 0)
			{
				mc = nPoint;
			}
			else
			{
				mc = pPoint;
			}
			mc.visible = true;
			BlackJackMain.updateValue(mc, Math.abs(value));
		}

		public function delaySubtotal(value:int):void
		{
			subtotal = value;
		}

		public function setPoint(mc:MovieClip):void
		{
			var tf:TextField;
			for (var i:int = 0; i < count; i++)
			{
				point.push(mc['Point' + i]);
				tf = point[i].Label;
				tf.defaultTextFormat = tf.getTextFormat();
				tf.text = '';
			}
		}

		public function setDealEnd(mc:MovieClip):void
		{
			dealend = mc;
		}

		public function setDeck(mc:MovieClip):void
		{
			for (var i:int = 0; i < count; i++)
			{
				deck.push(mc['Deck' + i]);
			}
		}

		public function get isSplit():Boolean
		{
			return _isSplit;
		}

		public function set isSplit(value:Boolean):void
		{
			_isSplit = value;
			if (value)
			{
				var u:uint;
				u = poker[0].pop();
				addPoker(u, 1);
				u = poker[0].pop();
				addPoker(u, 0);
				setPoker(0, 1, -1);
				setPoker(0, 0, -1);
				point[0].gotoAndStop(1);
				Point(0).text = '';
				clearTargetClip(Split + 0);
				setTargetClip(Split + 1, betNumber);
				setTargetClip(Split + 2, betNumber);
			}
		}

		public function set isInsurance(bool:Boolean):void
		{
			if (bool)
			{
				var value:int = betNumber / 2;
				setTargetClip(Insurance, value);
			}
		}

		public function set isSurrender(bool:Boolean):void
		{
			if (bool)
			{
				var value:int = betNumber / 2;
				setTargetClip(Split + 0, value);
			}
		}

		public function set isDouble(bool:Boolean):void
		{
			if (bool)
			{
				var value:int = betNumber * 2;
				setTargetClip(Split + 0, value);
			}
		}

		public function setSplit(value:Boolean):void
		{
			_isSplit = value;
		}

		public function get betNumber():int
		{
			return _betNumber;
		}

		public function Bet(value:int):void
		{
			_betNumber = value;
			setTargetClip(Split + 0, value);
		}
	}
}
